package com.pateo.bjev.contacts.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.pateo.bjev.contacts.BjevContactsManager;
import com.pateo.bjev.contacts.BtData;
import com.pateo.bjev.contacts.MainActivity;
import com.pateo.bjev.contacts.R;
import com.pateo.bjev.contacts.adapter.CallItemDecoration;
import com.pateo.bjev.contacts.adapter.CallLogAdapter;
import com.pateo.bluetooth.logic.bean.ContactLog;

import java.util.List;

/**
 * Created by pateo on 17-5-9.
 */

public class CallLogFragment extends Fragment{

    View contentView;
    BjevContactsManager manager;
    View no_connect_view;
    RecyclerView callLogList;
    CallLogAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.calllogs_layout,null);
        initView();
        return contentView;
    }

    private void initView(){
        manager = ((MainActivity)getContext()).getManager();
        callLogList = (RecyclerView) contentView.findViewById(R.id.calllog_list);

        //init listView
        callLogList.addItemDecoration(new CallItemDecoration(getContext(),LinearLayoutManager.HORIZONTAL,1, Color.WHITE));
        callLogList.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new CallLogAdapter(getContext(),null);
        callLogList.setAdapter(adapter);

        no_connect_view = contentView.findViewById(R.id.no_connect_calllog);
        contentView.findViewById(R.id.btn_connect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity main = (MainActivity) getActivity();
                main.toBtSetting();
            }
        });
        if(manager!=null){
            try {
                switch (manager.getBtState()){
                    case BtData.BT_STATE_CONNECTED:
                        switchCallLogs(true);
                        break;
                    case BtData.BT_STATE_DISCONNECTED:
                        switchCallLogs(false);
                        break;
                    default:
                        break;
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    public void switchCallLogs(boolean visible){
        if(!isAdded()){
            return;
        }
        Transition transition = TransitionInflater.from(getContext()).inflateTransition(R.transition.data_loaded);
        TransitionManager.beginDelayedTransition((ViewGroup)contentView,transition);
        if(visible){
            loadCallLogs();
            callLogList.setVisibility(View.VISIBLE);
            no_connect_view.setVisibility(View.GONE);
        }else {
            no_connect_view.setVisibility(View.VISIBLE);
            callLogList.setVisibility(View.GONE);
        }
    }

    private void loadCallLogs(){
        manager = ((MainActivity)getContext()).getManager();
        try {
            adapter.updateList(manager.getContactLogs());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void loadCallLogs(List<ContactLog> logs){
        adapter.updateList(logs);
    }
}
